/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		model:{
        			url:"api/cardispatch/query",
        			params:function(){
        				var subnav = widget.get("subnav");
        				return{
        					"carStatus":subnav.getValue("status"),
        					"orderString":"dispatchDate:DESC",
        					fetchProperties:"*,car.carType,car.carNumber,driver.personalInfo.name,driver.personalInfo.phone," +
        							"groupReserve.planItemGroup.pkPlanItemGroup," +
        							"groupReserve.planItemGroup.allianceOrg.name," +
									"groupReserve.planItemGroup.servicePoint.name,"+
        							"nonMemGroupReserve.nonMemberGroup.contactName," +
									"nonMemGroupReserve.nonMemberGroup.checkInDate," +
                            		"nonMemGroupReserve.nonMemberGroup.perNumber," +
                            		"nonMemGroupReserve.nonMemberGroup.checkOutDate," +
                            		"nonMemGroupReserve.nonMemberGroup.signUpNum," +
        							"nonMemGroupReserve.nonMemberGroup.servicePoint.name,"
        				}
        			},
        			columns:[{
        				name:"oddCode",
        				label:"单号"
        			},{
        				name:"carNumber",
        				label:"组团单位/预约人",
        				format:function(value,row){
        					if(row.groupReserve==null){
        						return	row.nonMemGroupReserve.nonMemberGroup.contactName;
        					}else{
        						if(row.groupReserve.planItemGroup.allianceOrg){
        							return row.groupReserve.planItemGroup.allianceOrg.name;
        						}else{
        							return row.groupReserve.planItemGroup.servicePoint.name
        						}
        					}
        				}
        			},{
        				name:"dispatchDate",
        				label:"用车时间",
        				format:"date"
        			},{
        				name:"unitsCar",
        				label:"本单位车",
        				format:function(row){
        					if(row){
        						return "是";
        					}else{
        						return "否";
        					}
        				}
        			},{
        				name:"driverName",
        				label:"司机名称",
        				format:function(value,row){
        					if(row.driverName){
        						return row.driverName;
        					}else if(row.driver){
        						return row.driver.personalInfo.name;
        					}else{
        						return " ";
        					}
        				}
        			},{
        				name:"driverPhone",
        				label:"司机联系电话",
        				format:function(value,row){
        					if(row.driverPhone){
        						return row.driverPhone;
        					}else{
        						return row.driver.personalInfo.phone;
        					}
        				}
        			},{
        				name:"carModel",
        				label:"车辆型号",
        					format:function(value,row){
            					if(row.carModel){
            						return row.carModel;
            					}else if(row.car){
            						return row.car.carType;
            					}else{
            						return " ";
            					}
            				}
        			},{
        				name:"carNumber",
        				label:"车牌号",
        				format:function(value,row){
        					if(row.car){
        						return row.car.carNumber;
        					}else{
        						return " ";
        					}
        				}
        			},{
        				name:"backDate",
        				label:"收车时间",
        				format:"date",
        				format:function(value,row){
        					if(row.backDate){
        						return moment(row.backDate).format("YYYY-MM-DD");
        					}else{
        						return " ";
        					}
        				}
        			},{
        				name:"carStatus",
        				label:"状态",
        				format:function(value,row){
        					if(row.carStatus){
        						if(row.carStatus.key=="Initial"){
            						return "初始";
            					}else if(row.carStatus.key=="Sent"){
            						return "已派车";
            					}else{
            						return "已收车";
            					}
        					}else{
        						return " " ;
        					}
        				}
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"open",
        					text:"派车",
        					show:function(index,data,rowEle){
        						if(data.carStatus){
        							if(data.carStatus.key=="Initial"){
            							return true;
            						}else{
            							return false;
            						}
        						}else{
        							return false;
        						}
							},
							handler:function(index,data,rowEle){
								var cParams = {
										pkCarDispatchBill: data.pkCarDispatchBill,
										version: data.version,
										carStatus:"Sent"
									};
								aw.saveOrUpdate("api/cardispatch/savecarbill",cParams,function(cParams){
									grid.refresh();
								});
							}
        				},{
							id:"back",
							text:"收车",
							show:function(index,data,rowEle){
								if(data.carStatus){
									if(data.carStatus.key=="Sent"||data.carStatus.key=="Sent"){
	        							return true;
	        						}else{
	        							return false;
	        						} 
        						}else{
        							return false;
        						}
							},
							handler:function(index,data,rowEle){
								var cParams = {
									pkCarDispatchBill: data.pkCarDispatchBill,
									version: data.version,
									carStatus:"Back",
									backDate:moment(new Date()).format("YYYY-MM-DD")
								};
							aw.saveOrUpdate("api/cardispatch/savecarbill",cParams,function(cParams){
								grid.refresh();
							});
							}
						},{
        					id:"edit",
        					text:"修改",
        					show:function(index,data,rowEle){
        						if(data.carStatus){
        							if(data.carStatus.key=="Initial"){
            							return true;
            						}else{
            							return false;
            						}
        						}else{
        							return false;
        						}
							},
        					handler:function(index,data,rowEle){
        						widget.get("subnav").hide(["search","add","status","carDate"]).show(["return"]);	
        						widget.get("form").setData(data);
        						widget.hide([".J-grid"]).show([".J-form"]);
        						var form = widget.get("form");
        						$(".el-form-buttons").removeClass("hidden");
        						form.setDisabled(false);
        						form.setValue("pkCarDispatchBill",data.pkCarDispatchBill);
        						form.setValue("version",data.version);
        						form.setValue("carStatus",data.carStatus.key);
        						if(data.unitsCar){
        							form.hide(["companyName","carModel","driverName"]);
        							form.setValue("carNumber",data.car.carNumber);
        						}else{
        							form.hide(["car","carNumber","driver"]).show(["companyName","driverName","carModel"]);
        							form.setValue("companyName",data.companyName);
        							form.setValue("carModel",data.carModel);
        							form.setValue("driverName",data.driverName);
        						}
        						if(data.groupReserve){
        							form.setValue("groupReserveN",data.groupReserve.planItemGroup.allianceOrg ==null? data.groupReserve.planItemGroup.servicePoint.name:data.groupReserve.planItemGroup.allianceOrg.name);
        							form.setValue("destination",data.groupReserve.planItemGroup.servicePoint.name);
        							form.setValue("checkInDate",moment(data.groupReserve.checkInDate).format("YYYY-MM-DD"));
        							form.setValue("checkOutDate",moment(data.groupReserve.checkOutDate).format("YYYY-MM-DD"));
//        							form.setValue("signUpNum",data.groupReserve.planItemGroup.signUpNum);
        							form.setValue("groupReserve",data.groupReserve.pkGroupReserve);
        						}else{
        							form.setValue("groupReserveN",data.nonMemGroupReserve.nonMemberGroup.contactName);
        							form.setValue("destination",data.nonMemGroupReserve.nonMemberGroup.servicePoint.name);
        							form.setValue("checkInDate",moment(data.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD"));
        							form.setValue("checkOutDate",moment(data.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD"));
//        							form.setValue("signUpNum",data.nonMemGroupReserve.nonMemberGroup.perNumber==null?data.nonMemGroupReserve.nonMemberGroup.signUpNum:data.nonMemGroupReserve.nonMemberGroup.perNumber);
        							form.setValue("nonMemGroupReserve",data.nonMemGroupReserve.pkNonMemGroupReserve);
        						}
        					}
        				},{
        					id:"delete",
        					text:"删除",
        					show:function(index,data,rowEle){
        						if(data.carStatus){
        							if(data.carStatus.key=="Initial"){
            							return true;
            						}else{
            							return false;
            						}
        						}
							},
        					handler:function(index,data,rowEle){
								aw.del("api/cardispatch/"+data.pkCarDispatchBill+"/delete",function() {
									grid.refresh();
								});	
							}
        				},{
        					id:"show",
        					text:"查看",
        					show:function(index,data,rowEle){
        						if(data.carStatus){
        							if(data.carStatus.key=="Sent" || data.carStatus.key=="Back"){
            							return true;
            						}else{
            							return false;
            						}
        						}else{
        							return false;
        						}
        						
							},
        					handler:function(index,data,rowEle){
        						widget.get("subnav").show(["return"]).hide(["search","add","status","carDate"]);	
        						widget.get("form").setData(data);
        						widget.hide([".J-grid"]).show([".J-form"]);
        						var form = widget.get("form");
        						$(".el-form-buttons").addClass("hidden");
        						form.setDisabled(true);
        						form.setValue("pkCarDispatchBill",data.pkCarDispatchBill);
        						form.setValue("version",data.version);
        						if(data.unitsCar){
        							form.hide(["companyName","carModel","driverName"]).show(["car","carNumber","driver"]);
        							form.setValue("carNumber",data.car.carNumber);
        							form.setValue("driverPhone",data.driver.personalInfo.phone);
        						}else{
        							form.hide(["car","carNumber","driver"]).show(["companyName","driverName","carModel"]);
        							form.setValue("companyName",data.companyName);
        							form.setValue("carModel",data.carModel);
        							form.setValue("driverName",data.driverName);
        						}
        						if(data.groupReserve){
        							form.setValue("groupReserveN",data.groupReserve.planItemGroup.allianceOrg ==null? data.groupReserve.planItemGroup.servicePoint.name:data.groupReserve.planItemGroup.allianceOrg.name);
        							form.setValue("destination",data.groupReserve.planItemGroup.servicePoint.name);
        							form.setValue("checkInDate",moment(data.groupReserve.checkInDate).format("YYYY-MM-DD"));
        							form.setValue("checkOutDate",moment(data.groupReserve.checkOutDate).format("YYYY-MM-DD"));
//        							form.setValue("signUpNum",data.groupReserve.planItemGroup.signUpNum);
        							form.setValue("groupReserve",data.groupReserve.pkGroupReserve);
        						}else{
        							form.setValue("groupReserveN",data.nonMemGroupReserve.nonMemberGroup.contactName);
        							form.setValue("destination",data.nonMemGroupReserve.nonMemberGroup.servicePoint.name);
        							form.setValue("checkInDate",moment(data.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD"));
        							form.setValue("checkOutDate",moment(data.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD"));
//        							form.setValue("signUpNum",data.nonMemGroupReserve.nonMemberGroup.perNumber==null?data.nonMemGroupReserve.nonMemberGroup.signUpNum:data.nonMemGroupReserve.nonMemberGroup.perNumber);
        							form.setValue("nonMemGroupReserve",data.nonMemGroupReserve.pkNonMemGroupReserve);
        						}
							}
        				}]
        			},]
        		},
        		
        	});
        	widget.set("grid",grid)
        }
    };
    module.exports = instance;
});
